<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">

<div id="app">
    <div class="content-header">
        <h1>系统管理
            <small>角色管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">

            <div class="filter-container">
                <el-input placeholder="角色名" style="width: 200px;" class="filter-item"  v-model="pagination.queryString"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建角色</el-button>
            </div>

            <el-table size="small" current-row-key="id" :data="list" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="角色名" align="center"></el-table-column>
                <el-table-column prop="keyword" label="角色关键词" align="center"></el-table-column>
                <el-table-column prop="description" label="角色描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
                        <el-button type="primary" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="add-form">
                <el-dialog title="新增角色" :visible.sync="dialogFormVisible">
                    <el-tabs v-model="activeName" type="card">
                        <el-tab-pane label="基本信息" name="first">
                            <el-form ref="roleAddForm" :model="formData" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="角色名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="角色关键字" prop="keyword">
                                            <el-input v-model="formData.keyword"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="22">
                                        <el-form-item label="角色描述" prop="description">
                                            <el-input type="textarea" v-model="formData.description"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                        </el-tab-pane>
                        <el-tab-pane label="权限信息" name="second">
                            <div class="checkScrol">
                                <table class="datatable">
                                    <thead>
                                    <tr>
                                        <th>选择</th>
                                        <th>权限名称</th>
                                        <th>关键词</th>
                                        <th>权限说明</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="permission in permissionList">
                                        <td>
                                            <input :id="permission.id" v-model="permissionIds" type="checkbox" :value="permission.id">
                                        </td>
                                        <td><label :for="permission.id">{{permission.name}}</label></td>
                                        <td><label :for="permission.id">{{permission.keyword}}</label></td>
                                        <td><label :for="permission.id">{{permission.description}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </el-tab-pane>
                        <el-tab-pane label="菜单信息" name="third">
                            <div class="checkScrol">
                                <table class="datatable">
                                    <thead>
                                    <tr>
                                        <th>选择</th>
                                        <th>角色名称</th>
                                        <th>关键词</th>
                                        <th>角色说明</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="menu in menuList">
                                        <td>
                                            <input :id="menu.id" v-model="menuIds" type="checkbox" :value="menu.id">
                                        </td>
                                        <td><label :for="menu.id">{{menu.name}}</label></td>
                                        <td><label :for="menu.id">{{menu.keyword}}</label></td>
                                        <td><label :for="menu.id">{{menu.description}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </el-tab-pane>
                    </el-tabs>


                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <div class="add-form">
                <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                    <el-tabs v-model="activeName" type="card">
                        <el-tab-pane label="基本信息" name="first">
                            <el-form ref="roleAddForm" :model="formData" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="角色名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="角色关键字" prop="keyword">
                                            <el-input v-model="formData.keyword"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="22">
                                        <el-form-item label="角色描述" prop="description">
                                            <el-input type="textarea" v-model="formData.description"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                        </el-tab-pane>
                        <el-tab-pane label="权限信息" name="second">
                            <div class="checkScrol">
                                <table class="datatable">
                                    <thead>
                                    <tr>
                                        <th>选择</th>
                                        <th>权限名称</th>
                                        <th>关键词</th>
                                        <th>权限说明</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="permission in permissionList">
                                        <td>
                                            <input :id="permission.id" v-model="permissionIds" type="checkbox" :value="permission.id">
                                        </td>
                                        <td><label :for="permission.id">{{permission.name}}</label></td>
                                        <td><label :for="permission.id">{{permission.keyword}}</label></td>
                                        <td><label :for="permission.id">{{permission.description}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </el-tab-pane>
                        <el-tab-pane label="菜单信息" name="third">
                            <div class="checkScrol">
                                <table class="datatable">
                                    <thead>
                                    <tr>
                                        <th>选择</th>
                                        <th>角色名称</th>
                                        <th>关键词</th>
                                        <th>角色说明</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="menu in menuList">
                                        <td>
                                            <input :id="menu.id" v-model="menuIds" type="checkbox" :value="menu.id">
                                        </td>
                                        <td><label :for="menu.id">{{menu.name}}</label></td>
                                        <td><label :for="menu.id">{{menu.keyword}}</label></td>
                                        <td><label :for="menu.id">{{menu.description}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </el-tab-pane>
                    </el-tabs>


                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleUpdate()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            list: [],
            formData: [],
            dialogFormVisible: false,
            dialogFormVisible4Edit: false,
            dialogFormVisible4PermissionEdit: false,
            permissionList: [],
            permissionIds: [],
            menuList:[],
            menuIds:[],
            activeName:"first"
        },
        mounted() {
            /*axios.get('/role/findAll.do').then(resp => (

                this.list = resp.data.data.list
            ))*/
            this.findPage();
        },
        methods: {
            handleEdit(id) {
                this.resetForm();
                this.dialogFormVisible4Edit = true;

                //默认选中第一个标签页
                this.activeName = 'first';
                //发送ajax请求根据id查询检查组信息，用于基本信息回显
                axios.get("/role/findById.do?id="+id).then((res)=>{
                    if (res.data.flag){
                        this.formData = res.data.data;
                    }else {
                        this.$message.error("获取数据失败，请刷新后重试")
                    }
                });
                //权限
                axios.get("/permission/findAll.do").then((res)=>{
                    if (res.data.flag){
                        //将检查项列表数据赋值给模型数据用于页面表格展示
                        this.permissionList=res.data.data;
                        //查询当前检查组包含的所有检查项id，用于页面回显,后台只需要传一个List<Integer>
                        axios.get("/role/findPermissionIdsByRoleId.do?id="+id).then((res)=>{
                            //为模型数据赋值，通过VUE数据双向绑定进行信息的回显
                            if(res.data.flag){
                                this.permissionIds = res.data.data;
                            }else{
                                this.$message.error(res.data.message);
                            }
                        })
                    }else {
                        this.$message.error(res.data.message);
                    }
                });
                //菜单
                axios.get("/menu/findAll.do").then((res)=>{
                    if (res.data.flag){
                        //将检查项列表数据赋值给模型数据用于页面表格展示
                        this.menuList=res.data.data;
                        //查询当前检查组包含的所有检查项id，用于页面回显,后台只需要传一个List<Integer>
                        axios.get("/role/findMenuIdsByRoleId.do?id="+id).then((res)=>{
                            //为模型数据赋值，通过VUE数据双向绑定进行信息的回显
                            if(res.data.flag){
                                this.menuIds = res.data.data;
                            }else{
                                this.$message.error(res.data.message);
                            }
                        })
                    }else {
                        this.$message.error(res.data.message);
                    }
                });
            },
            // 重置表单
            resetForm() {
                //重置表单数据
                this.formData={};
                //重置复选框选项
                this.permissionIds = [];
                this.menuIds = []
            },
            handleUpdate() {
                axios.post("/role/edit.do?permissionIds="+this.permissionIds+"&menuIds="+this.menuIds,this.formData).then((res)=>{
                    if (res.data.flag){
                        //编辑成功
                        this.$message({
                            type: 'success',
                            message: res.data.message
                        });
                    }else {
                        //编辑失败
                        this.$message.error(res.data.message);
                    }
                }).finally(()=>{
                    //关闭窗口
                    this.dialogFormVisible4Edit=false;
                    //查询
                    this.findPage();
                })
            },

            handleCreate() {
                this.dialogFormVisible = true;

                //重置窗口
                this.resetForm();

                //默认切换到第一个标签页（基本信息）
                this.activeName = 'first';
                //发送ajax请求，查询所有检查项信息，赋值为tableData
                axios.get("/permission/findAll.do").then((res)=>{
                    if (res.data.flag){
                        //将检查项列表数据赋值给模型数据用于页面表格展示
                        this.permissionList=res.data.data;
                    }else {
                        this.$message.error(res.data.message);
                    }
                });
                axios.get("/menu/findAll.do").then((res)=>{
                    if (res.data.flag){
                        //将检查项列表数据赋值给模型数据用于页面表格展示
                        this.menuList=res.data.data;
                    }else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            handleAdd() {
                //发送ajax请求将模型数据提交到后台处理
                axios.post("/role/add.do?permissionIds="+this.permissionIds+"&menuIds="+this.menuIds,this.formData).then(res=>{
                    //关闭新增窗口
                    this.dialogFormVisible = false;
                    if(response.data.flag){
                        //新增成功，弹出成功提示
                        this.$message({
                            message: response.data.message,
                            type: 'success'
                        });
                    }else{
                        //新增失败，弹出错误提示
                        this.$message.error(response.data.message);
                    }
                }).finally(()=>{
                    this.findPage();
                })
            },
            handleDelete(row) {
                this.$confirm('确认删除该条记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get("/role/delete.do?id="+row).then((res)=>{
                        if (res.data.flag){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.findPage();
                        } else {
                            this.$message.error(res.data.message);
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            findPage() {
                //封装分页参数
                let param = {
                    currentPage:this.pagination.currentPage,
                    pageSize:this.pagination.pageSize,
                    queryString:this.pagination.queryString
                };
                //发送ajax请求，提交分页相关参数
                axios.post("/role/findPage.do",param).then((res)=>{
                    //为模型数据赋值，基于VUE的双向绑定展示到页面
                    this.pagination.total=res.data.total;
                    /*if (this.pagination.total <= 10) {
                        this.handleCurrentChange(1);
                    }*/
                    this.list=res.data.rows;
                })
            },
            handleCurrentChange(currentPage) {
                //currentPage为切换后的页码
                this.pagination.currentPage=currentPage;
                this.findPage();
            }
        }
    })
</script>
</html>